<?php
namespace app\erp_meixingtiyu\model;

use think\Model;
use app\common\model\UploadFile;
use app\common\enum\order\PayStatus as OrderPayStatusEnum;
use app\common\model\User;

class CourseOrder extends Model
{
    protected $table = 'athlon_course_order';
    protected $pk = 'order_id';

    public function image()
    {
        return $this->hasOne(UploadFile::class, 'file_id', 'image_id');
    }

    public function coach()
    {
      return $this->hasOne(Coach::class, 'employee_id', 'employee_id');
    }

    public function user()
    {
      return $this->hasOne(User::class, 'user_id', 'user_id');
    }

    public function getPayTimeAttr($value)
    {
      return date('Y-m-d H:i:s', $value);
    }

    public static function getList()
    {
      return self::with(['image', 'coach', 'user'])->where('pay_status', OrderPayStatusEnum::SUCCESS)->order('pay_time desc')->paginate(15);
    }

    public function getAllStudentsByCourseId($courseId)
    {
      return self::with(['user', 'user.avatar'])->where('pay_status', OrderPayStatusEnum::SUCCESS)->where('course_id', $courseId)->select();
    }

    public static function getListByDate($startDate, $endDate)
    {
      return self::with(['image', 'coach', 'user'])->where('pay_status', OrderPayStatusEnum::SUCCESS)->whereBetweenTime('pay_time', $startDate, $endDate)->order('pay_time desc')->select();
    }

    public function getOrdersByDate($date) 
    {
        return $this->where('pay_status', OrderPayStatusEnum::SUCCESS)->whereDay('pay_time', $date)->select();
    }
}